package com.yymmarket.forum.user;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.yymmarket.util.db.DbOperation;

public class UserDAO {

	private static final String ADD_USER = "insert into forum_user values(?,?,?,?,?,?,?)";
	public boolean addUser(UserBean bean) {
		
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		if(!db.prepareStatement(ADD_USER)) {
			db.release();
			return false;
		}
		PreparedStatement ps = db.getPStmt();
		try {
			ps.setString(1, bean.getAccount());
			ps.setString(2, bean.getPassword());
			ps.setString(3, bean.getUserName());
			ps.setLong(4, bean.getPhone());
			ps.setLong(5, System.currentTimeMillis());
			ps.setShort(6, bean.getGender());
			ps.setShort(7, bean.getRank());
			ps.executeUpdate();
			
			bean.setId(db.getLastInserId("forum_user"));
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			db.release();
		}
		
		return true;
	}
	
	
	private static final String DELETE_USER_BY_ACCOUNT = "delete from forum_user where account = ?";
	public boolean deleteUser(String account) {
		
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		if(!db.prepareStatement(DELETE_USER_BY_ACCOUNT)) {
			db.release();
			return false;
		}
		PreparedStatement ps = db.getPStmt();
		try {
			ps.setString(1, account);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}finally{
			db.release();
		}
		
		return true;
	}
	
	
	private static final String GET_USER_BY_ACCOUNT = "select * from forum_user where account = ?";
	public UserBean getUserByAccount(String account) {
		
		DbOperation db = new DbOperation(DbOperation.DB_WAP);
		
		if(!db.prepareStatement(GET_USER_BY_ACCOUNT)) {
			db.release();
			return null;
		}
		try {
			
			db.getPStmt().setString(1, account);
			ResultSet rs = db.getPStmt().executeQuery();
			if (rs.next()) {
				return getUser(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			db.release();
		}

		return null;
	}
	
	private static final String GET_USER_BY_ID = "select * from forum_user where id = ?";
	public UserBean getUserById(int id) {
		DbOperation db = new DbOperation(DbOperation.DB_WAP);

		if (!db.prepareStatement(GET_USER_BY_ID)) {
			db.release();
			return null;
		}
		try {

			db.getPStmt().setInt(1, id);
			ResultSet rs = db.getPStmt().executeQuery();
			if (rs.next()) {
				return getUser(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			db.release();
		}

		return null;
	}
	
	
	private UserBean getUser(ResultSet rs) throws SQLException{
		UserBean bean = new UserBean();
		bean.setId(rs.getInt("id"));
		bean.setAccount(rs.getString("account"));
		bean.setPassword(rs.getString("password"));
		bean.setUserName(rs.getString("user_name"));
		bean.setPhone(rs.getLong("phone"));
		bean.setRegisterTime(rs.getLong("register_time"));
		bean.setGender(rs.getShort("gender"));
		bean.setRank(rs.getShort("rank"));
		
		return bean;
	}
}
